<?php
namespace app\index\controller;

use app\index\model\KuaiDiHelpOrderModel;

class Order extends Base
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V5.1<br/><span style="font-size:30px">12载初心不改（2006-2018） - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
    }

    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }

    public function getHelpOrderList() {
        if(empty($this->userId)) {
            $res['success'] = false;
            $res['desc'] = '登录态失效';
            $res['errorcode'] = '200022';
            return json($res);
        }
        $page = input('page', 1);
        $size = input('size', 10);
        $userName = input('userName', '');
        $tel = input('tel', '');
        $code = input('code', '');
        $ordeo_no = input('ordeo_no', '');
        $model = new KuaiDiHelpOrderModel();
        $where = [];
        $where['user_id'] = ['=', $this->userId];
        if($tel) {
            $where['tel'] = ['like', '%' . $tel . '%']; 
        }
        if ($userName) {
            $where['user_name'] = ['like', '%' . $userName . '%'];
        }
        if($code) {
            $where['code'] = ['=', $code];
        }
        if($ordeo_no) {
            $where['ordeo_no'] = ['=', $ordeo_no];
        }
        $res = $model->getHelpOrderList($where, $page, $size);
        return json($res);
    }

    public function addHelpOrder() {
        if(empty($this->userId)) {
            $res['success'] = false;
            $res['desc'] = '登录态失效';
            $res['errorcode'] = '200022';
            return json($res);
        }
        $orderStr = input('orderList', '');
        $data = [];
        if(empty($orderStr)) {
            $res['success'] = false;
            $res['desc'] = '参数错误';
            $res['errorcode'] = '200022';
            return json($res);
        }
        $orderList = json_decode($orderStr, true);
        if(empty($orderList)) {
            $msg = json_last_error_msg();
            $res['success'] = false;
            $res['desc'] = '参数错误' . $msg;
            $res['errorcode'] = '200022';
            return json($res);
        }
        foreach($orderList as $val) {
            if(empty($val['tel'])) {
                $res['success'] = false;
                $res['desc'] = '手机号不能为空';
                $res['errorcode'] = '200022';
                return json($res);
            }
            if(empty($val['userName'])) {
                $res['success'] = false;
                $res['desc'] = '用户名称不能为空';
                $res['errorcode'] = '200022';
                return json($res);
            }
            if(empty($val['code']) && empty($val['order_no'])) {
                $res['success'] = false;
                $res['desc'] = '取件码或者快定订单号不能为空';
                $res['errorcode'] = '200022';
                return json($res);
            }
            if(empty($val['optionId'])) {
                $res['success'] = false;
                $res['desc'] = '快递不能为空';
                $res['errorcode'] = '200022';
                return json($res);
            }
            $data[] = [
                'option_id' => $val['optionId'],
                'code' => isset($val['code']) ? $val['code'] : "",
                'order_no' => isset($val['orderNo']) ? $val['orderNo'] : "",
                'user_name' => isset($val['userName']) ? $val['userName'] : "",
                'tel' => isset($val['tel']) ? $val['tel'] : "",
                'user_id' => $this->userId,
                'note' => isset($val['note']) ? $val['note'] : "",
            ];
        }
        if ($data) {
            $model = new KuaiDiHelpOrderModel();
            $res = $model->addHelpOrder($data);
        } else {
            $res['success'] = false;
            $res['desc'] = '数据格式有误';
            $res['errorcode'] = '200022';
        }
        return json($res);

    }
}
